﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Xml;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;

public partial class dangnhap : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txttaikhoan.Focus();
         if (Session["taikhoan"] != null)
        {
            Response.Redirect("Default.aspx");
        }
    }

   
    public int Kiemtra(string id, string pass)
    {

        pass = MaHoaMatKhau(pass);
        ConnectionData conn = new ConnectionData();
        string sql = "select * from khach_hang where tai_khoan = '" + id + "' and mat_khau = '" + pass + "'";    
    
        if (conn.LayDangNhap(sql)==1)
            return 1;
        else
            return 0;
    }


    public string MaHoaMatKhau(string password)
    {
        UnicodeEncoding encoding = new UnicodeEncoding();
        Byte[] hashBytes = encoding.GetBytes(password);
        // Compute the SHA-1 hash
        SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
        Byte[] cryptPassword = sha1.ComputeHash(hashBytes);
        return BitConverter.ToString(cryptPassword);
    }

    protected void btndangnhap_Click(object sender, EventArgs e)
    {
        //KiemTraNhap(txttaikhoan.Text + "", MaHoaMatKhau(txtmatkhau.Text).Trim());
         int i = -1;
        //Kiemtra KTUserPass = new Kiemtra();
         if (txttaikhoan.Text == "" || txtmatkhau.Text == "")
         {
             lblThongbao.Text = "Tên đăng nhập hoặc mật khẩu không được để trống !";
         }
         else
         {
             i = Kiemtra(txttaikhoan.Text, txtmatkhau.Text);
             if (i != 0)
             {
                 Session["taikhoan"] = txttaikhoan.Text;
                 
                 Response.Redirect("Default.aspx");
             }
             else
             {
                 lblThongbao.Text = "Tên đăng nhập hoặc mật khẩu không đúng !";
             }             
         }        
    }
    
}

